package com.manager.commons.basemanage.struts.action;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.manager.commons.basemanage.commons.Constants;
import com.manager.commons.basemanage.entity.BaseData;
import com.manager.commons.basemanage.entity.BaseDataList;
import com.manager.commons.basemanage.service.BaseDataService;
import com.manager.commons.basemanage.service.BaseManageService;
import com.manager.commons.basemanage.struts.form.BaseDataForm;
import com.manager.commons.basemanage.struts.form.BaseDataListForm;
import com.manager.commons.exception.BaseException;

/**
 * 
 * Copyrights 1997-2008 西安协同时光软件有限公司<br />
 * 
 * Filename:BaseDataListAction.java<br />
 * 
 * Description:BaseDataList管理的Action，BaseDataList管理的入口<br />
 * 
 * Version:V0.1<br />
 * 
 * Author: peigen<br />
 * 
 * History: <br /> [ Author Date Version Content ]<br />
 * 					peigen 2008-07-29 V0.1 create <br />
 * 
 */
public class BaseDataListAction extends DispatchAction {
	/**
	 * Service层实例
	 */
	private BaseManageService baseManageService;
	private BaseDataService baseDataService;

	/**
	 * 日志
	 */
	private Logger log = Logger.getLogger(this.getClass());

	/**
	 * 实体对象
	 */
	private BaseDataList baseDataList;
	private BaseData baseData;

	// 增删改查
	/**
	 * 获取BaseDataList列表
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 * @throws Exception
	 */
	public ActionForward listBaseDataList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws BaseException, IllegalAccessException,
			InvocationTargetException {
		log.debug("getBaseDataListList");
		BaseDataList baseDataList = this.getBaseDataList(form);

		List<BaseDataList> baseDataListList = (List) baseManageService.getAllObject(baseDataList);
		request.setAttribute(Constants.BASEDATALISTLIST, baseDataListList);

		return mapping.findForward(Constants.SUCCESS);
	}
	
	/**
	 * 获取BaseDataList列表根据BaseDataList的TId
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 * @throws Exception
	 */
	public ActionForward listBaseDataListForBaseDataID(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws BaseException, IllegalAccessException,
			InvocationTargetException {
		BaseDataList baseDataList = this.getBaseDataList(form);

		List<BaseDataList> baseDataListList = (List) baseDataService.getBaseDataListList(baseDataList.getTId());
		request.setAttribute(Constants.BASEDATALISTLIST, baseDataListList);
		request.setAttribute(Constants.BASEDATALIST, baseDataList);
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 显示BaseDataList信息
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 */
	public ActionForward showBaseDataList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IllegalAccessException, InvocationTargetException {

		BaseDataList baseDataList = this.getBaseDataList(form);
		baseDataList = (BaseDataList) this.baseManageService.getObject(baseDataList, baseDataList.getId());

		request.setAttribute(Constants.BASEDATALIST, baseDataList);

		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 跳转到新增BaseDataList页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 */
	public ActionForward toAddBaseDataList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws IllegalAccessException, InvocationTargetException {
		BaseDataList baseDataList = this.getBaseDataList(form);
		baseData = new BaseData();
		baseData = (BaseData) baseDataService.getObject(baseData, baseDataList.getTId());
		request.setAttribute(Constants.BASEDATALIST, baseDataList);
		request.setAttribute(Constants.BASEDATA, baseData);
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 新增BaseDataList
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	public ActionForward addBaseDataList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IllegalAccessException, InvocationTargetException {
		BaseDataList baseDataList = this.getBaseDataList(form);
		this.baseManageService.insertObject(baseDataList);
		request.setAttribute(Constants.BASEDATALIST, baseDataList);
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 跳转到修改BaseDataList页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 */
	public ActionForward toUpdateBaseDataList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IllegalAccessException, InvocationTargetException {
		BaseDataList baseDataList = this.getBaseDataList(form);
		baseDataList = (BaseDataList) this.baseManageService.getObject(baseDataList, baseDataList.getId());
		request.setAttribute(Constants.BASEDATALIST, baseDataList);
		
		baseData = new BaseData();
		baseData = (BaseData) baseDataService.getObject(baseData, baseDataList.getTId());
		request.setAttribute(Constants.BASEDATA, baseData);
		
		return mapping.findForward(Constants.SUCCESS);
	}
	
	
	public ActionForward searchBaseDataList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws BaseException, IllegalAccessException,
			InvocationTargetException {
			BaseDataListForm baseDataListForm = (BaseDataListForm)form;
			BaseDataList baseDataList=new BaseDataList();
			baseDataList.setTId(baseDataListForm.getTId());
		List<BaseDataList> baseDataListList = (List) this.baseDataService.getBaseDataDao().searchBaseDataList(baseDataListForm.getSelectValue(),baseDataListForm.getTId().toString());
		request.setAttribute(Constants.BASEDATALISTLIST, baseDataListList);
		request.setAttribute(Constants.BASEDATALIST, baseDataList);
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 修改BaseDataList
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	public ActionForward updateBaseDataList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IllegalAccessException, InvocationTargetException {
		BaseDataList baseDataList = this.getBaseDataList(form);
		this.baseManageService.updateObject(baseDataList);
		request.setAttribute(Constants.BASEDATALIST, baseDataList);
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 删除BaseDataList
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	public ActionForward delBaseDataList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IllegalAccessException, InvocationTargetException {
		BaseDataList baseDataList = this.getBaseDataList(form);
		this.baseManageService.deleteObject(baseDataList);
		request.setAttribute(Constants.BASEDATALIST, baseDataList);
		return mapping.findForward(Constants.SUCCESS);
	}

	// get--set
	/**
	 * 获取BaseDataList实体
	 * 
	 * @param ActionForm
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 * @return the BaseDataList
	 */
	public BaseDataList getBaseDataList(ActionForm form) throws IllegalAccessException,
			InvocationTargetException {
		BaseDataListForm baseDataListForm = (BaseDataListForm) form;
		BaseDataList baseDataList = new BaseDataList();
		BeanUtils.copyProperties(baseDataList, baseDataListForm);
		return baseDataList;
	}

	/**
	 * @param baseDataList
	 *            the baseDataList to set
	 */
	public void setBaseDataList(BaseDataList baseDataList) {
		this.baseDataList = baseDataList;
	}

	public BaseManageService getBaseManageService() {
		return baseManageService;
	}

	public void setBaseManageService(BaseManageService baseManageService) {
		this.baseManageService = baseManageService;
	}

	public BaseDataService getBaseDataService() {
		return baseDataService;
	}

	public void setBaseDataService(BaseDataService baseDataService) {
		this.baseDataService = baseDataService;
	}

	/**
	 * 获取BaseData实体
	 * 
	 * @param ActionForm
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 * @return the BaseDataList
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 */
	public BaseData getBaseData(ActionForm form) throws IllegalAccessException, InvocationTargetException {
		BaseDataForm baseDataForm = (BaseDataForm) form;
		BaseData baseData = new BaseData();
		BeanUtils.copyProperties(baseData, baseDataForm);
		
		return baseData;
	}
	
	public BaseDataList getBaseDataListForm(ActionForm form) throws IllegalAccessException, InvocationTargetException {
		BaseDataListForm baseDataListForm = (BaseDataListForm) form;
		BaseDataList  baseDataList = new BaseDataList();
		BeanUtils.copyProperties(baseDataList, baseDataListForm);
		
		return baseDataList;
	}

	public void setBaseData(BaseData baseData) {
		this.baseData = baseData;
	}
}